capture log close
capture noisily log using "R:\Zentrale\ZB-S\Daten_FDSZ_Gastforscher\1_gafo\2016_0133\Table 3.log", replace
capture noisily log using "Table 3.log", replace

**** Load data in Stata
clear all
set more off
set maxvar 5000
capture noisily use "final_sample_03032023_pseudo.dta", clear
capture noisily use "R:\Zentrale\ZB-S\Daten_FDSZ_Gastforscher\1_gafo\2016_0133\data\Bundesbank Discussion Paper\final_sample_03032023.dta", clear




* Calculate event-time: 
*quietly br id smallsample land datum sanctions sysnr notFATF_C am100
quietly sort id datum
quietly by id: gen temp = datum if sanctions - sanctions[_n-1] == 1
quietly by id: egen temp2 = max(temp)
quietly gen event_time = datum - temp2
quietly drop temp temp2






* Calculate positions, monthly growth rates, and volatility at the land level: 
preserve

sort land datum sysnr


* Calculate aggregated positions by asset class:
capture noisily egen AM100aggr = total(am100), by(land datum)

capture noisily gen am100ex123 = am100 - am123
capture noisily egen AM123aggr = total(am123), by(land datum) // loans only
capture noisily egen AM100ex123aggr = total(am100ex123), by(land datum) // non loans

qui duplicates drop land datum, force
capture noisily xtset land datum


* Calculate growth rates 
capture noisily gen gr_AM100aggr = (AM100aggr-L.AM100aggr) / L.AM100aggr
capture noisily gen gr_AM100ex123aggr = (AM100ex123aggr-L.AM100ex123aggr) / L.AM100ex123aggr
capture noisily gen gr_AM123aggr = (AM123aggr-L.AM123aggr) / L.AM123aggr


* Calculate sd of monthly growth rates:
capture noisily by land: egen sdgr_AM100aggr = sd(gr_AM100aggr)
capture noisily by land: egen sdgr_AM100ex123aggr = sd(gr_AM100ex123aggr)
capture noisily by land: egen sdgr_AM123aggr = sd(gr_AM123aggr)




* Table 3:
* Sumstats of (vola of growth rate of) aggregated position in country c:


* Panel A
capture noisily tabstat AM100aggr gr_AM100aggr, statistics(count mean sd skew p10 p50 p90) columns(statistics) varwidth(24)

by land: gen temp = _n==1
capture noisily tabstat sdgr_AM100aggr if temp==1, statistics(count mean sd skew p10 p50 p90) columns(statistics) varwidth(24)


* Panel B
capture noisily tabstat AM100aggr gr_AM100aggr if smallsample==0, statistics(count mean sd skew p10 p50 p90) columns(statistics) varwidth(24)

capture noisily tabstat sdgr_AM100aggr if temp==1 & smallsample==0, statistics(count mean sd skew p10 p50 p90) columns(statistics) varwidth(24)


* Panel C
capture noisily tabstat AM100aggr if smallsample==1, statistics(count mean sd skew p10 p50 p90) columns(statistics) varwidth(24)

capture noisily tabstat AM100aggr if smallsample==1 & event_time<0, statistics(count mean sd skew p10 p50 p90) columns(statistics) varwidth(24)

capture noisily tabstat AM100aggr if smallsample==1 & event_time>=0, statistics(count mean sd skew p10 p50 p90) columns(statistics) varwidth(24)

capture noisily tabstat gr_AM100aggr if smallsample==1, statistics(count mean sd skew p10 p50 p90) columns(statistics) varwidth(24)

capture noisily tabstat sdgr_AM100aggr sdgr_AM123aggr sdgr_AM100ex123aggr if temp==1 & smallsample==1, statistics(count mean sd skew p10 p50 p90) columns(statistics) varwidth(24)

drop temp

restore 






log close
